//
//  EditMyInfoViewController.m
//  FactorySale
//
//  Created by 主事丫环 on 2020/1/13.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "EditMyInfoViewController.h"
#import "EditMyInfoTableViewCell.h"
#import "BRPickerView.h"
#import "ZzyTextField.h"
#import "NetLoadAreaPickerViewController.h"
#import "EditPhoneViewController.h"
#import "SYBigImage.h"
#import "UserAgreementController.h"
@interface EditMyInfoViewController ()<UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topNavBarConstraint;

@property (weak, nonatomic) IBOutlet UIImageView *headerImage;
@property (weak, nonatomic) IBOutlet UITextField *cjmcTf;//厂家名称
@property (weak, nonatomic) IBOutlet UITextField *lxrTf;//联系人
@property (weak, nonatomic) IBOutlet UITextField *lxfsTf;//联系方式
@property (weak, nonatomic) IBOutlet UITextField *yxTf;//邮箱
@property (weak, nonatomic) IBOutlet UITextField *cjdzTF;//厂家地址
@property (weak, nonatomic) IBOutlet UITextField *xxdzConLab;//详细地址
@property (weak, nonatomic) IBOutlet UIImageView *zhiZhaoImg;

@property (nonatomic, strong) NSString *shengshiquString;
@property (nonatomic, strong) NSString *shengshiquCode;
@property (nonatomic, strong) NSString *phoneStr;//
@property (nonatomic, assign) BOOL headImgUpLoad;

@end

@implementation EditMyInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.topNavBarConstraint.constant = kNaviHeight;
    
    [self setupView];
    
    [self getFactoryInfoData]; //获取厂家用户信息
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}
#pragma mark - 私有方法
- (void)setupView {
    self.headerImage.layer.borderColor = WHITECOLOR.CGColor;
    self.headerImage.layer.borderWidth = 1;
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeHeaderImg:)];
    [self.headerImage addGestureRecognizer:tapGesture];
    
    UITapGestureRecognizer *yyzzlabTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yyzzlabTapGesture:)];
    [self.zhiZhaoImg addGestureRecognizer:yyzzlabTapGesture];
}
/**
头像 点击事件处理
*/
-(void)changeHeaderImg:(UITapGestureRecognizer *)sender{
    self.headImgUpLoad = YES;
    UIImagePickerController *pick = [[UIImagePickerController alloc]init];
    pick.allowsEditing = YES;
    pick.delegate = self;
    [UIAlertController showPhotoAlerControllerPicker:pick controller:self sourceView:nil];
}
- (void)upLoadHeadIcon:(UIImage *)image {
    if (self.headImgUpLoad ==YES) {
        self.headerImage.image = image;
        [MBProgressHUD showHUDAddedTo:self.view msg:@"请稍后..." animated:YES];
    }
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [NetWorkTool UploadPicWithUrl:updateFactoryUser param:param key:@"headImg" image:image withSuccess:^(id dic) {
        [self getFactoryInfoData];
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}
- (IBAction)leftBtnClick {
    [self.navigationController popViewControllerAnimated:YES];
}
/**
 修改联系电话
 */
- (IBAction)lxfsTfTap {
    EditPhoneViewController *vc = [[EditPhoneViewController alloc]init];
    vc.phoneEditHandle = ^(NSString * _Nonnull phoneStr) {
        self.phoneStr = phoneStr;
        self.lxfsTf.text = phoneStr;
    };
    [self.navigationController pushViewController:vc animated:YES];
}
/**
 用户协议点击
 */
- (IBAction)userAgreementButtonClick {
    UserAgreementController * vc = [[UserAgreementController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
/**
 营业执照 点击事件处理
 */
-(void)yyzzlabTapGesture:(UITapGestureRecognizer *)sender{
    self.headImgUpLoad = NO;
    UIImagePickerController *pick = [[UIImagePickerController alloc]init];
    pick.allowsEditing = YES;
    pick.delegate = self;
    [UIAlertController showPhotoAlerControllerPicker:pick controller:self sourceView:nil];
}
/**
保存数据
*/
- (IBAction)saveBtnClick {
    [self saveDataRequest];
}

#pragma mark - 网络请求
/**
 获取厂家用户信息 数据请求
 */
- (void)getFactoryInfoData {
    [MBProgressHUD showHUDAddedTo:self.view msg:@"请稍后..." animated:YES];
    
    [NetWorkTool POST:getFactoryInfo param:nil success:^(id dic) {
        
        NSDictionary *dataDic = dic[@"data"];
        self.cjmcTf.text = dataDic[@"factory_name"]; //厂家名称
        self.lxrTf.text = dataDic[@"factory_person"]; //联系人
        self.lxfsTf.text = dataDic[@"factory_phone"]; //联系方式
        self.yxTf.text = dataDic[@"factory_email"]; //邮箱
        NSArray *array = [dataDic[@"factory_address"] componentsSeparatedByString:@" "];
        self.cjdzTF.text = array[0]; //厂家地址
        self.xxdzConLab.text = dataDic[@"factory_address"]; //详细地址
        NSString *headImgStr = dataDic[@"factory_img"];
        [self.headerImage sd_setImageWithURL:[NSURL URLWithString:headImgStr] placeholderImage:nil];
        
        NSString *zhiZhaoStr = dataDic[@"factory_licence"];
        [self.zhiZhaoImg sd_setImageWithURL:[NSURL URLWithString:zhiZhaoStr] placeholderImage:nil];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    } other:^(id dic) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } needUser:YES];
}
/**
保存 数据请求
*/
- (void)saveDataRequest {
    
    [MBProgressHUD showHUDAddedTo:self.view msg:@"保存中..." animated:YES];
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    param[@"factory_phone"] = self.phoneStr; //联系方式
    param[@"factory_name"] = self.cjmcTf.text; //厂家名称
    param[@"factory_person"] = self.lxrTf.text; //负责人名字
    param[@"factory_area"] = self.shengshiquCode; //厂家地址所在区域（县级代码）
    param[@"factory_address"] = self.xxdzConLab.text; //详细地址
    param[@"factory_email"] = self.yxTf.text; //邮箱地址
    param[@"headImg"] = self.headerImage.image; //头像
    param[@"licenceImg"] = self.zhiZhaoImg.image; //营业执照
    [NetWorkTool UploadPicWithUrl:updateFactoryUser param:param key:@"headImg" image:self.headerImage.image withSuccess:^(id dic) {
        //KMyLog(@"保存成功");
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

- (ZzyTextField *)getTextField:(UITableViewCell *)cell {
    ZzyTextField *textField = [[ZzyTextField alloc]initWithFrame:CGRectMake(80, 0, KWIDTH-100, 44)];
    textField.backgroundColor = [UIColor clearColor];
    textField.font = [UIFont systemFontOfSize:16.0f];
    textField.textAlignment = NSTextAlignmentLeft;
    textField.delegate = self;
    [cell.contentView addSubview:textField];
    return textField;
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage *image = info[UIImagePickerControllerEditedImage];
    
    [self upLoadHeadIcon:image];
    [picker dismissViewControllerAnimated:YES completion:nil];
}

//选择厂家地址 点击事件处理
- (void)selectCjdz:(UITapGestureRecognizer *)sender {
    NetLoadAreaPickerViewController * loadAreaVC = [[NetLoadAreaPickerViewController alloc]init];
    
    loadAreaVC.selectedBlock = ^(NetLoadAreaModel *areaModel) {
        //KMyLog(@"选中的地址%@",areaModel);
        self.shengshiquString = [NSString stringWithFormat:@"%@%@%@", areaModel.provinceName, areaModel.cityName, areaModel.districtName];
        //省市区 code拼接
        self.shengshiquCode = [NSString stringWithFormat:@"%@",areaModel.districtCode];
        self.cjdzTF.text = self.shengshiquString;
    };
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) {
        loadAreaVC.modalPresentationStyle=UIModalPresentationOverCurrentContext;
    } else {
        loadAreaVC.modalPresentationStyle=UIModalPresentationCurrentContext;
    }
    [self presentViewController:loadAreaVC animated:NO completion:^{
        
    }];
    return;
}

@end
